import math from '@/utils/math';

export function priceFormat(price, format = 1, isMoneyFilter = false) {
  if (Number(price)) {
    const data = (Number(price) / 100 + 0.001).toFixed(format);
    return isMoneyFilter ? moneyFilter(data) : data;
  } else {
    return price;
  }
}

/*
* 数字格式化，超过 10000 显示 1万
*/
export function numFormat(num, isShowUnit = true, fix = 1, NumUnit = 'w') {
  let formatNum = Number(num);
  if (formatNum) {
    if (formatNum >= 10000) {
      const unit = isShowUnit ? NumUnit : '';
      formatNum = (formatNum / 10000).toFixed(fix) + unit;
    } else if (!Number.isInteger(formatNum)) {
      formatNum = formatNum.toFixed(fix);
    }
    return formatNum;
  } else {
    return num;
  }
}

// 数字格式化，超过1000 显示 1k 超过10000 显示w
export function detailNumFormat(num, isShowUnit = true, fix = 1) {
  let formatNum = Number(num);
  if (formatNum) {
    if (formatNum >= 1000) {
      const unit = isShowUnit ? 'k' : '';
      formatNum = (formatNum / 1000).toFixed(fix) + unit;
    } else if (formatNum >= 10000) {
      const unit = isShowUnit ? 'w' : '';
      formatNum = (formatNum / 10000).toFixed(fix) + unit;
    } else if (!Number.isInteger(formatNum)) {
      formatNum = formatNum.toFixed(fix);
    }
    return formatNum;
  } else {
    return num;
  }
}

export function bigNumFilter(numData) {
  if (numData || numData === 0) {
    const num = Number(numData);
    if (num >= 0 && num <= 9999) {
      return moneyFilter(num);
    } else if (num >= 10000 && num <= 9999999) {
      return moneyFilter(Math.floor(num / 10000)) + 'w';
    } else if (num >= 10000000) { //  && num < 99999999999
      const numStr = Math.floor(num).toString();
      return moneyFilter(numStr.substr(0, numStr.length - 7)) + 'kw';
    } else if (num === 0) {
      return 0;
    }
    // else if (num >= 100000000000) {
    //   return moneyFilter(Math.floor(num / 100000000000)) + '千亿';
    // }
  } else {
    return '--';
  }
}
/**
 *
 * @param {Number | String} numData 数字
 * @param {Boolean} substr 是否最多两位小数点
 * @param {String} unit 单位
 * @param {Boolean} padEnd 是否补0
 * @param {String} fixed 保留小数位数，为null则不处理
 * @returns {String}
 */
export function bigDataFilter(numData, substr = true, unit = '', padEnd = false, fixed = 1) {
  if (numData || numData === 0) {
    const num = Number(numData);
    if (num > 0 && num <= 9999) {
      // return moneyFilter(num);
      return moneyFilter(num, substr, unit, padEnd);
    } else if (num >= 10000 && num <= 99999999) {
      return divide(num, 10000, fixed) + '万';
    } else if (num >= 100000000) { //  && num < 99999999999
      return divide(num, 100000000, fixed) + '亿';
    } else if (num === 0) {
      return 0;
    } else if (num < 0 && num >= -9999) {
      return moneyFilter(num, substr, unit, padEnd);
    } else if (num <= -10000) {
      return divide(num, 10000, fixed) + '万';
    } else if (num <= -100000000) { //  && num < 99999999999
      return divide(num, 100000000, fixed) + '亿';
    }
  } else {
    return '--';
  }
}
function divide(num1, num2, fixed = null) {
  if (num2 === 0) return num1;
  const res = math.divide(num1, num2);
  if (fixed === null) {
    return res;
  }
  return res.toFixed(fixed);
}
export function bigNumFloorFilter(numData, floorNum = 2) {
  if (numData || numData === 0) {
    let isMinus = false;
    if (numData < 0) {
      isMinus = true;
    }
    const num = Number(Math.abs(numData));
    if (num >= 0 && num <= 9999) {
      return `${isMinus ? '-' : ''}${moneyFilter(num, true, '', true)}`;
    } else if (num >= 10000 && num <= 9999999) {
      return `${isMinus ? '-' : ''}` + moneyFilter((num / 10000).toFixed(floorNum)) + '万';
    } else if (num >= 10000000) { //  && num < 99999999999
      const numStr = (num / 10000000).toFixed(floorNum);
      return `${isMinus ? '-' : ''}` + moneyFilter(numStr) + '千万';
    } else if (num === 0) {
      return 0;
    }
  } else {
    return '--';
  }
}

export function thousandFormat(num, isShowUnit = true, fixed = true, moneyUnit = 'w') {
  let formatNum = Number(num);
  if (formatNum) {
    const unit = isShowUnit ? moneyUnit : '';
    formatNum = fixed ? (formatNum / 10000).toFixed(1) + unit : formatNum / 10000 + unit;
    return formatNum;
  } else {
    return num;
  }
}

export function isNumberEmpty(num, unit = '') {
  if (num) {
    return `${num}${unit}`;
  } else if (num === 0) {
    return `${num}${unit}`;
  } else {
    return '--';
  }
}
/**
 *
 * @param {Number | String} money 金额
 * @param {Boolean} substr 是否最多两位小数点
 * @param {String} unit 单位
 * @param {Boolean} padEnd 是否补0
 * @param {String} defaultResult 默认值
 * @returns {String}
 */
export function moneyFilter(money, substr = true, unit = '', padEnd = false, defaultResult = '--') {
  if (money && money != null) {
    const isNegative = Number(money) < 0;
    money = String(money);
    const left = money.split('.')[0];
    let right = money.split('.')[1];
    if (substr) {
      // 是否补0
      if (padEnd) {
        right = right ? right.padEnd(2, '0') : '00';
      }
      right = right ? (right.length >= 2 ? '.' + right.substr(0, 2) : '.' + right + '0') : '';
    } else {
      right = right ? '.' + right : '';
    }
    var temp = left.split('').reverse().join('').match(/(\d{1,3})/g);
    if (temp === null) return defaultResult;
    return (isNegative ? '-' : '') + temp.join(',').split('').reverse().join('') + right + unit;
  } else if (money === 0) {
    if (padEnd) {
      return '0.00' + unit;
    }
    return money + unit;
  } else {
    return defaultResult;
  }
}

/*
* 数字格式化，超过 10000 显示 万
*/
export function numberTextFormat(num, fix = 1) {
  let formatNum = Number(num);
  if (!formatNum) return num;
  let flag = 1;
  if (formatNum < 0) {
    flag = 0;
  }
  formatNum = Math.abs(formatNum);
  if (formatNum) {
    if (formatNum >= 10000 && formatNum < 100000000) {
      formatNum = (formatNum / 10000).toFixed(fix) + '万';
    } else if (formatNum >= 100000000) {
      formatNum = (formatNum / 100000000).toFixed(fix) + '亿';
    } else if (!Number.isInteger(formatNum)) {
      formatNum = formatNum.toFixed(fix);
    }
    return flag ? formatNum : '-' + formatNum;
  } else {
    return num;
  }
}

/*
* 数字格式化，中文单位
*/
export function bigNumberZH(num) {
  let formatNum = Math.abs(num);
  const sign = Math.sign(num);
  // 参数为正数，返回+1；
  // 参数为负数，返回-1；
  // 参数为 0，返回0；
  // 参数为-0，返回-0;
  // 其他值，返回NaN。
  if (formatNum) {
    if (formatNum >= 1000000000000) {
      formatNum = (formatNum / 1000000000000).toFixed(2) + '万亿';
    } else if (formatNum >= 100000000) {
      formatNum = (formatNum / 100000000).toFixed(2) + '亿';
    } else if (formatNum >= 10000) {
      formatNum = (formatNum / 10000).toFixed(2) + '万';
    }
    return sign === -1 ? `-${formatNum}` : formatNum;
  } else {
    return num;
  }
}
